// 利用多态实现电脑cpu，显卡，内存组装
#include <iostream>
using namespace std;
class Cpu
{
public:
    virtual void calculate() = 0;
};
class VideoCard
{
public:
    virtual void display() = 0;
};
class Memory
{
public:
    virtual void store() = 0;
};
class Computer
{
private:
    Cpu *m_cpu;
    VideoCard *m_vc;
    Memory *m_mem;

public:
    Computer();
    Computer(Cpu *cpu, VideoCard *vc, Memory *mem);
    ~Computer();
    void doWork()
    {
        this->m_cpu->calculate();
        this->m_vc->display();
        this->m_mem->store();
    }
};
Computer::Computer() {}
Computer::Computer(Cpu *cpu, VideoCard *vc, Memory *mem)
{
    this->m_cpu = cpu;
    this->m_vc = vc;
    this->m_mem = mem;
}
Computer::~Computer()
{
    if (m_cpu != nullptr)
    {
        delete m_cpu;
        m_cpu = nullptr;
    }
    if (m_vc != nullptr)
    {
        delete m_vc;
        m_vc = nullptr;
    }
    if (m_mem != nullptr)
    {
        delete m_mem;
        m_mem = nullptr;
    }
}
class IntelCpu : public Cpu
{
public:
    void calculate()
    {
        cout << "Intel calculate" << endl;
    }
};
class IntelVideoCard : public VideoCard
{
public:
    void display()
    {
        cout << "Intel display" << endl;
    }
};
class IntelMemory : public Memory
{
public:
    void store()
    {
        cout << "Intel store" << endl;
    }
};
class LenovoCpu : public Cpu
{
public:
    void calculate()
    {
        cout << "Lenovo calculate" << endl;
    }
};
class LenovoVideoCard : public VideoCard
{
public:
    void display()
    {
        cout << "Lenovo display" << endl;
    }
};
class LenovoMemory : public Memory
{
public:
    void store()
    {
        cout << "Lenovo store" << endl;
    }
};
void test1()
{
    Computer *c1 = new Computer(new IntelCpu, new IntelVideoCard, new IntelMemory);
    c1->doWork();
    delete c1;
    cout << "--------------" << endl;
    c1 = new Computer(new LenovoCpu, new IntelVideoCard, new LenovoMemory);
    c1->doWork();
    delete c1;
    c1 = nullptr;
}
int main(void)
{
    test1();

    return 0;
}